package com.zy.teacher.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.commont.result.Result;
import com.zy.commont.result.ResultCode;
import com.zy.teacher.entity.EduTeacher;
import com.zy.teacher.entity.query.TeacherQuery;
import com.zy.teacher.exception.EduException;
import com.zy.teacher.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.PasswdUserDatabase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import sun.reflect.generics.tree.VoidDescriptor;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/teacher")
@CrossOrigin //跨域请求
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("/list")
    public Result getList() {
        //int i=1/0; 再次提交合并代码1234   master1
        try {
            List<EduTeacher> list = eduTeacherService.list(null);
            return Result.ok().data("items", list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "删除")
//   @GetMapping("id")
    @DeleteMapping("{id}")
    public Result del(@PathVariable(value = "id") @ApiParam(name = "id", value = "讲师id") String tid) {
        try {
            eduTeacherService.removeById(tid);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "讲师分页查询")
    @GetMapping("/{page}/{limt}")
    public Result TeacherPage(
            @PathVariable(value = "page") @ApiParam(name = "page", value = "当前页") Integer page,
            @PathVariable(value = "limt") @ApiParam(name = "limt", value = "每页显示条数") Integer limt) {
        try {
            Page<EduTeacher> teacherPage = new Page<>(page, limt);
            eduTeacherService.page(teacherPage, null);
            List<EduTeacher> records = teacherPage.getRecords();
            long total = teacherPage.getTotal();
            return Result.ok().data("total", total).data("list", records);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //条件查询的分页
    // 1、 有分页
    // 2、条件 ： 根据讲师名称、讲师等级、创建时间、修改时间
    @ApiOperation(value = "根据讲师条件分页查询")
    @PostMapping("/{page}/{limit}")
    public Result selectTeacherByPageAndWrapper(
            @ApiParam(name="page",value = "当前页", required = true)
            @PathVariable(value = "page") Integer page,
            @ApiParam(name="limit",value = "每页显示记录数", required = true)
            @PathVariable Integer limit,
            @RequestBody TeacherQuery query){
        try {
            Page<EduTeacher> teacherPage = new Page<>(page, limit);
            //Wrapper<TeacherQuery> queryWrapper = new QueryWrapper<TeacherQuery>();
            eduTeacherService.pageQuery(teacherPage, query);
            return Result.ok().data("total",teacherPage.getTotal()).data("rows",teacherPage.getRecords());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "保存讲师对象")
    @PostMapping("save")
    public Result saveTeacher(@RequestBody EduTeacher teacher){
        try {
            eduTeacherService.save(teacher);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "根据ID查询")
    @GetMapping("{id}")
    public Result selectTeacherById(
            @ApiParam(name="id",value = "讲师ID", required = true)
            @PathVariable  String id){

          //当我们的业务被非法参数操作时、我们可以自定义异常（业务异常）
        EduTeacher teacher = eduTeacherService.getById(id);
        if(teacher == null){
            throw new EduException(ResultCode.EDU_ID_ERROR,"没有此讲师信息");
        }

        try {
//            EduTeacher teacher = eduTeacherService.getById(id);
            return Result.ok().data("teacher",teacher);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "修改讲师信息")
    @PutMapping("update")
    public Result selectTeacherById(@RequestBody EduTeacher teacher){
        try {
            eduTeacherService.updateById(teacher);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

}

